var passport = require('passport');

module.exports = function (app) {

    app.get('/auth', function (req, res) {

        if (req.isAuthenticated()) {
            res.redirect('/');
            return;
        }

        res.render('auth', {
            error: req.flash('error')
        });
    });

    app.get('/sign-out', function (req, res) {
        req.logout();
        res.redirect('/');
    });

    app.post('/auth', passport.authenticate('local', {
        successRedirect: '/',
        failureRedirect: '/auth',
        failureFlash: true })
    );

    app.get('/auth/fb',
        passport.authenticate('facebook', {
            scope: 'read_stream'
        })
    );

    app.get('/auth/fb/callback',
        passport.authenticate('facebook', {
            successRedirect: '/',
            failureRedirect: '/auth' })
    );

    app.get('/auth/vk',
        passport.authenticate('vk', {
            scope: ['friends']
        }),
        function (req, res) {
            // The request will be redirected to vk.com for authentication, so
            // this function will not be called.
            console.log(res);
        });

    app.get('/auth/vk/callback',
        passport.authenticate('vk', {
            failureRedirect: '/auth'
        }),
        function (req, res) {
            // Successful authentication, redirect home.
            var accountId = res.req.user.profileUrl;
            console.log(accountId);
            // if exists
            res.redirect('/');
        });

	app.get('/auth/twitter', 
		passport.authenticate('twitter'),
		function (req, res){

		});

	app.get('/auth/twitter/callback', 
		passport.authenticate('twitter', {
			failureRedirect: '/auth'
		}),
        function(req, res){
            res.redirect("/");
        });

    app.get('/auth/google',
        passport.authenticate('google', { scope: ['https://www.googleapis.com/auth/userinfo.profile',
            'https://www.googleapis.com/auth/userinfo.email'] }),
        function(req, res){
        });

    app.get('/auth/google/callback',
        passport.authenticate('google', { failureRedirect: '/login' }),
        function(req, res) {
            res.redirect('/');
        });

    app.get('/auth/instagram',
        passport.authenticate('instagram'),
        function(req, res){
        });

    app.get('/auth/instagram/callback',
        passport.authenticate('instagram', { failureRedirect: '/login' }),
        function(req, res) {
            res.redirect('/');
        });

    app.get('/auth/foursquare',
        passport.authenticate('foursquare'),
        function(req, res){
            // The request will be redirected to Foursquare for authentication, so this
            // function will not be called.
        });

    app.get('/auth/foursquare/callback',
        passport.authenticate('foursquare', { failureRedirect: '/login' }),
        function(req, res) {
            res.redirect('/');
        });
}

